/*
 * @Author: 徐建辰
 * @Date: 2022-06-01 15:10:59
 * @LastEditTime: 2022-06-01 15:13:30
 * @LastEditors: 徐建辰
 * @Description: 文件操作函数
 */
/**
 * @desc: base64转blob
 * @param {WindowBase64} dataURI base64
 * @return {Blob}
 */
 export const dataURItoBlob = dataURI => {
  const byteString = atob(dataURI.split(',')[1])
  const ab = new ArrayBuffer(byteString.length)
  const ia = new Uint8Array(ab)
  for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i)
  }
  return new Blob([ia], { type: 'image/png' })
}

/**
 * @desc: blob 转文 件对象
 * @param {Blob} newBlob blob格式文件
 * @param {String} fileName 文件名
 * @param {String} fileType 文件类型
 * @return {File}
 */
export const blobToFile = (newBlob, fileName, fileType= 'image/png') => {
  return new File([newBlob], fileName, {
    type: fileType,
    lastModified: Date.now(),
  })
}

/**
 * @desc: base64 转 file
 * @param {String} src 图片base64
 * @param {String} fileName 文件名
 * @return {void}
 */
export const dataURIToFile = (src, fileName) => {
  const blob = dataURItoBlob(src)
  const file = blobToFile(blob, fileName)
  return file
}